package cn.getech.data.metadata.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

@Slf4j
public class SingleConnect {


    private static SingleConnect single = null;
    private SingleConnect() {
    }
    /**
     * 获取实例
     */
    public static SingleConnect getInstance(){
        if (single == null) {
            synchronized (SingleConnect.class) {
                if (single == null) {
                    single = new SingleConnect();
                }
            }
        }
        return single;
    }


    public Connection getConnection(String serverUrl, String userName, String password, String driverClass) {
        Connection conn = null;
        try {
            Properties props = new Properties();
            props.put("useInformationSchema", "true");
            props.put("user", userName);
            props.put("password", password);
            Class.forName(driverClass);
            DriverManager.setLoginTimeout(5);
            conn = DriverManager.getConnection(serverUrl, props);
        } catch (Exception e) {
           log.error("获取连接错误",e);
        }
        return conn;
    }
}
